package main

import (
	"context"
	"fmt"
	"github.com/asim/go-micro/plugins/wrapper/trace/opentracing/v3"
	"github.com/asim/go-micro/v3"
	"github.com/asim/go-micro/v3/client"
	"github.com/zw2582/ginlib"
	"learn.micro.service.product/common"
	"learn.micro.service.product/proto/product"
)

//模拟客户端
func main() {
	//初始化ini配置
	ginlib.InitIni()

	//初始化日志
	ginlib.InitLogger()

	//创建jaeger链路追踪器
	t, o := common.CreateJaegerFromIni()
	defer o.Close()

	//define server and init
	ser := micro.NewService(
		//micro.Name(ginlib.APP_NAME),

		//配置consul注册中心
		micro.Registry(common.CreateRegistryFromConsul()),

		//定义client的选项
		micro.Client(client.NewClient(
			client.ContentType("application/grpc+proto"),

			//全局跳过负载均衡，直连服务端
			func(o *client.Options) {
				o.CallOptions.Address = []string{"127.0.0.1:8080"}
				}),
		),
		//配置jaeger链路追踪
		micro.WrapClient(opentracing.NewClientWrapper(t)),

		//配置熔断
		micro.WrapClient(common.NewHystrixClientWrapper("0.0.0.0:7979")),

		// 配置负载均衡,默认轮询
		//micro.Selector(selector.DefaultSelector),

	)

	ser.Init()

	//配置服务实例
	productSer := product.NewProductService(ginlib.APP_NAME, ser.Client())

	//调用服务方法,某个特殊方法跳过负载均衡，直连服务端
	resp, err := productSer.ProductAdd(context.TODO(), &product.ProductInfo{}, client.WithAddress("127.0.0.1:8080"))
	if err != nil {
		panic(err)
	}

	fmt.Println("新增商品结果", resp.Id)
}
